//
// Created by dong on 2025/10/20.
//

#include "SButton.h"

#include "Swindow.h"

SButton::SButton(const std::string& text, Swidget* parent):Swidget(parent)
{
    m_text=text;
    m_data->w=100;
    m_data->h=25;
    m_data->isVisible=true;
}

void SButton::paintEvent(SDL_Event* msg)
{
    Swidget::paintEvent(msg);
    TTF_Font* font = TTF_OpenFont("../simkai.ttf",20);
    SDL_Surface* surface=TTF_RenderUTF8_Blended(font,m_text.c_str(),m_data->textColor);
    SDL_Texture* temp = SDL_CreateTextureFromSurface(Swindow::render,surface);
    int tw  =surface->w;
    int th = surface->h;
    SDL_Rect dst={m_data->x +(m_data->w-tw)/2,m_data->y+(m_data->h-th)/2,tw,th};
    SDL_RenderCopy(Swindow::render,temp,NULL,&dst);
    SDL_FreeSurface(surface);
    TTF_CloseFont(font);
}

void SButton::mousePressEvent(SDL_Event* msg)
{
   if (func && Swidget::contains(msg->motion.x,msg->motion.y) && Swidget::getvisible())
   {
       func();

   }
}

void SButton::mouseMoveEvent(SDL_Event* msg)
{
   if (Swidget::contains(msg->motion.x,msg->motion.y))
   {
       m_data->isFocus=true;
   }else
   {
       m_data->isFocus=false;
   }
}
